import axios from "axios"
import errorCodes from "./errorCodes"


export default class Dao {
	constructor(config = {}) {
		this.config = config
		this.ajax = axios.create(this.config)
		this.throwStates = {}
		// this.throwState[errorCodes.InternalError] = true
		this.throwStates[errorCodes.UserNoSignin] = true
	}

	async get(path , params,noThrow = false) {
		var {data} = await axios.request({url:path , method:"get" , params:params})
		if(!noThrow)this.checkState(data)
		return data
	}
	async post(path ,form ,  params,noThrow = false) {
		var sp = form
		if("object"==typeof(form)) {
			sp = new URLSearchParams();
			for(var k in form) {
				sp.append(k,form[k])
			}
		}
		var {data} = await axios.request({url:path , method:"post" ,data:sp, params:params})
		if(!noThrow)this.checkState(data)
		return data
	}
	async upload(path ,form ,  params,noThrow = false) {
		var {data} = await axios.request({url:path , method:"post" ,data:form, params:params , headers:{'Content-Type': 'multipart/form-data'}})
		if(!noThrow)this.checkState(data)
		return data
	}

	checkState(data) {
		if(this.throwStates[data.State]) {
			throw data
		}
	}

	
}